package com.yy.wga.admin.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.admin.request.AdminMerchantMpConfigPageRequest;
import com.yy.wga.mp.module.admin.request.AdminMerchantMpConfigSaveRequest;
import com.yy.wga.mp.module.admin.request.AdminMerchantMpConfigSetBuyCommissionRequest;
import com.yy.wga.mp.module.admin.response.AdminMerchantMpConfigInfoResponse;
import com.yy.wga.mp.module.merchant.entity.pojo.MerchantInfoDO;
import com.yy.wga.mp.module.merchant.service.MerchantInfoService;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 运营后台/商户信息配置
 *
 * @author guochao
 * @since 2024-10-25 21:46:26
 */
@RestController
@RequestMapping("admin/merchant")
public class AdminMerchantController {
    /**
     * 服务对象
     */
    @Resource
    private MerchantInfoService merchantInfoService;


    @PostMapping("page")
    public Result<PageBean<AdminMerchantMpConfigInfoResponse>> page(@Valid @RequestBody AdminMerchantMpConfigPageRequest request) {
        PageBean<AdminMerchantMpConfigInfoResponse> pageBean = merchantInfoService.pageData(request);
        return Result.success(pageBean);
    }

    /**
     * 查询商户信息
     * @param appKey
     * @return
     */
    @GetMapping("get")
    public Result<AdminMerchantMpConfigInfoResponse> page(@RequestParam("appKey") String appKey) {
        MerchantInfoDO merchantInfo = merchantInfoService.getOne(new LambdaQueryWrapper<MerchantInfoDO>()
                .eq(MerchantInfoDO::getAppKey, appKey));
        return Result.success(BeansUtils.map(merchantInfo, AdminMerchantMpConfigInfoResponse.class));
    }

    @PostMapping("save")
    public Result<Boolean> save(@Valid @RequestBody AdminMerchantMpConfigSaveRequest request) {
        merchantInfoService.saveData(request);
        return Result.success(Boolean.TRUE);
    }

    /**
     * 设置自购省开关
     * @param request
     * @return
     */
    @PostMapping("setBuyCommission")
    public Result<Boolean> setBuyCommission(@Valid @RequestBody AdminMerchantMpConfigSetBuyCommissionRequest request) {
        merchantInfoService.update(new LambdaUpdateWrapper<MerchantInfoDO>()
                .eq(MerchantInfoDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .set(MerchantInfoDO::getBuyCommission, request.getBuyCommission()));
        return Result.success(Boolean.TRUE);
    }

    @GetMapping("del")
    public Result<Boolean> del(@RequestParam("id") Long id) {
        merchantInfoService.removeById(id);
        return Result.success(Boolean.TRUE);
    }
}
